<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>机构管理</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-3 col-sm-4 col-xs-12">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary ">
                    <i class="widget-icon typcn typcn-th-large-outline"></i>
                    <span class="widget-caption">组织机构</span>
                </div>
                <div class="widget-body">
                    <ul id="orgTree" class="ztree" style="width: auto; overflow: auto;"></ul>
                </div>
            </div>
        </div>
        <div class="col-lg-9 col-sm-8 col-xs-12">
            <div class="widget  no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon typcn typcn-th-list"></i>
                    <span class="widget-caption">机构管理</span>
                </div>
                <div class="widget-body">
                    <table id="grid-data">
                        <thead>
                        <tr>
                            <th style="text-align: center;width: auto;">中文组织机构名称</th>
                            <th style="text-align: center;width: auto;">英文组织机构名称</th>
                            <th style="text-align: center; width: auto;">描述</th>
                            <th style="width: 200px; text-align: center;">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="row-no-display">
                            <td style="text-align: center;">$orgName$</td>
                            <td style="text-align: center;">$orgEnName$</td>
                            <td style="text-align: center;">$remark$</td>
                            <td class="text-left" style="text-align: center;">
                                <a rel='editOrg' class="btn btn-info btn-xs edit"><i
                                        class="fa fa-edit"></i> 编辑</a>
                                <a rel='deleteOrg' class="btn btn-danger btn-xs edit"><i
                                        class="fa fa-times"></i> 删除</a>

                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade form" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
            <div class="widget no-padding">
                <div class="widget-header bg-themeprimary ">
                    <i class="widget-icon glyphicon glyphicon-credit-card"></i>
                    <span class="widget-caption">组织机构管理</span>
                    <div class="widget-buttons">
                        <a tabindex="7" data-type="reset"><i class="fa fa-refresh"></i></a>
                        <a data-dismiss="modal">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body">
                    <div>
                        <form class="form-horizontal" role="form" id="editForm">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="orgName">中文名称</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="orgName" name="orgName" type="text"
                                          placeholder="中文名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="orgEnName">英文名称</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="orgEnName" name="orgEnName" type="text"
                                           placeholder="中文名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="orgEnName">上级机构</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="parentName" name="parentName" type="text"
                                           disabled="disabled" >
                                    <input type="hidden" id="parentId" name="parentId">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="code">机构代码</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="code" name="code" type="text"
                                           placeholder="机构代码">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="sort">序号</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="sort" name="sort" type="text"
                                           placeholder="序号">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="remark">备注</label>
                                <div class="col-sm-10">
                                    <input class="form-control" id="remark" name="remark" type="text"
                                           placeholder="备注">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-8 col-sm-4" style="text-align: right">
                                    <a class="btn btn-success" href="#" id="btnSave"><i class="fa fa-check right"></i>保存</a>
                                    <a class="btn btn-danger" href="#" data-dismiss="modal"><i class="fa fa-times right"></i>关闭</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
    </div>
</div>
<script type="text/javascript">
    var orgObj = {
        insert: true,
        orgTreeObj: null,
        currentOrg: null,
        currentTreeNode: null,
        tempTreeNode: null,
        validateParam: {
            submitBtn:$("#btnSave"),
            submitHandler: function (obj) {
                obj.insert = orgObj.insert;
                if (!orgObj.insert) {
                    obj.id = orgObj.currentOrg.id;
                }
                orgObj.tempTreeNode = orgObj.currentTreeNode;
                obj.parentIds = "";
                obj.orgType='0';
                while (orgObj.tempTreeNode.getParentNode()) {
                    obj.parentIds += orgObj.tempTreeNode.id + ',';
                    orgObj.tempTreeNode = orgObj.tempTreeNode.getParentNode();
                }
                obj.parentIds += orgObj.tempTreeNode.id;
                $('#btnSave').button('loading');
                $.request("/admin/sys/org/save", obj, function (result) {
                    if (result) {
                        $('#editmodal').modal('hide');
                        bootbox.alert('保存成功！');
                        initOrg();
                        orgObj.grid.ext.refresh();
                    } else {
                        bootbox.alert('保存失败！');
                    }
                }).always(function () {
                    orgObj.validateParam.resetBtn.trigger("reset");
                    $('#btnSave').button('reset');
                });
            },
            fields: {
                orgName: {
                    validators: {
                        notEmpty: {}
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {},
                        dataType: {
                            dataType: 'decimal',
                            message: '只能输入数字'
                        }
                    }
                }
            }
        },
        grid: {
            keys: ['orgName'],
            url: "/admin/sys/org/query/page/parentId",
            placeholder: '机构名称',
            stableparm: function () {
                return {parentId: orgObj.currentTreeNode.id};
            },
            events: {
                editOrg: function (row) {
                    orgObj.insert = false;
                    orgObj.currentOrg = row;
                    orgObj.validateParam.resetBtn.trigger("reset");
                    $("#parentName").val(orgObj.currentTreeNode.orgName);
                    $.setobject(row, 'editForm');
                    $('#editmodal').modal('show');
                }, deleteOrg: function (row) {
                    bootbox.confirm('确认删除？', function () {
                        deleteRowList([id = row.id]);
                    });
                }
            },
            tools: [{
                text: '新增', img: 'fa fa-plus', event: function (selectItems) {
                    orgObj.insert = true;
                    orgObj.validateParam.resetBtn.trigger("reset");
                    if (orgObj.currentTreeNode != undefined || orgObj.currentTreeNode != null) {
                        $("#parentName").removeAttr("readOnly");
                        $("#id").val('0');
                        $("#parentName").val(orgObj.currentTreeNode.orgName);
                        $("#parentId").val(orgObj.currentTreeNode.id);
                    }
                    $('#editmodal').modal('show');
                }
            }, {
                text: '编辑', img: 'glyphicon glyphicon-edit', event: function (selectItems) {
                    if (selectItems && selectItems.length == 1) {
                        orgObj.insert = false;
                        orgObj.validateParam.resetBtn.trigger("reset");
                        updateRow(selectItems[0]);
                    }
                    else {
                        bootbox.alert('请选择一条记录！');
                    }
                }
            }, {
                text: '删除', img: 'glyphicon glyphicon-remove', event: function (selectItems) {
                    if (selectItems && selectItems.length > 0) {
                        bootbox.confirm('确认删除？', function () {
                            var ids = [];
                            $.each(selectItems, function (i, row) {
                                ids.push(row.id);
                            });
                            deleteRowList(ids);
                        });
                    } else {
                        bootbox.alert('请选择待删除的记录！');
                    }
                }
            }]
        }
    }
    function initOrg() {
        orgObj.orgTreeObj = $('#orgTree').tree({
            data: {
                key: {name: 'orgName', enname: 'orgEnName'},
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPId: 0
                }
            },
            callback: {
                onClick: onClick,
            }
        });
        if (orgObj.currentTreeNode) {
            orgObj.orgTreeObj.selectNode(orgObj.currentTreeNode = orgObj.orgTreeObj.getNodeByParam('id', orgObj.currentTreeNode.id));
        } else {
            orgObj.orgTreeObj.selectNode(orgObj.currentTreeNode = orgObj.orgTreeObj.getNodes()[0]);
        }
    }

    function onClick(event, treeId, treeNode, clickFlag) {
        orgObj.insert = false;
        orgObj.currentTreeNode = treeNode;
        orgObj.grid.ext.refresh();
    }
    function updateRow(obj) {
        orgObj.currentOrg = obj;
        obj.parentId = orgObj.currentTreeNode.id;
        obj.parentName = orgObj.currentTreeNode.orgName;
        $.setobject(obj, 'editForm');
        $('#editmodal').modal('show');
    }
    function deleteRowList(ids) {
        $.request('/admin/sys/org/delete/ids', {ids: ids}, function () {
            bootbox.alert("删除成功!");
            initOrg();
            orgObj.grid.ext.refresh();
        }, function () {
            bootbox.alert("删除失败!");
        })
    }
    function init() {
        initOrg();
        $("#grid-data").table(orgObj.grid);
        $('#editForm').validate(orgObj.validateParam);
    }
    function dispose() {
        orgObj = {};
    }
</script>
</body>
</html>